﻿package DevilCat
{

	import flash.display.MovieClip;
	import fl.transitions.Tween;
	import fl.transitions.easing.*;
	import fl.transitions.TweenEvent;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.events.Event;
	import flash.utils.clearInterval;
	import flash.utils.setInterval;
	import flash.ui.Keyboard;
	import flash.display.Loader;
	import flash.net.URLRequest;
	import flash.net.URLLoader;
	import DevilCat.*;
	import DevilCat.PVPBattle;

	public class MainObject extends MovieClip
	{
		var uid:Number = 0;
		var usecurity:Number = Math.round(Math.random() * 99999);
		var randomURL:Number = Math.round(Math.random() * 99999);
		var username:String;
		var uflag:String;
		var leadpkm:Number = 0;
		var pmp:Number = 0;
		var gameEventList:Array=new Array();
		var pokemonEventList:Array;
		var trainerEventList:Array;
		var npcEventList:Array;

		var wildPokemonList:Array;

		var tweenList:Array;

		var reloadMode:String = "new";


		var pixelCount:Number = 0;
		var speed:Number = 0;
		var speedPow:Number = 7;
		var friction = 0.9;
		var menuIsShowed:Boolean = true;
		var isShowPokemon:Boolean = false;
		var rankIsShowed:Boolean = false;
		var mapswapIsShowed:Boolean = false;
		var pokedexIsShowed:Boolean = false;
		var pokemonIsShowed:Boolean = false;
		var PVPIsShowed:Boolean = false;
		var itemIsShowed:Boolean = false;


		var eventWalkingIsAdded:Boolean = false;
		var trainerIsStanding:Boolean = true;
		var trainerIsJumping:Boolean = false;
		var trainerIsGoingLeft:Boolean = true;
		var trainerIsGoingRight:Boolean = false;


		var intervalId:uint;
		var atutoPvpRespond:uint;
		var flagCheck:uint;
		var curentMap:String = "SheepRanch";
		var newMap:String = "SheepRanch";

		var mapPokemon:XMLList;
		var mapEvent:XMLList;
		var totalPokemonInMap:Number;

		public function MainObject()
		{

			addEventListener(Event.ENTER_FRAME,preLoad);
			preLoadtxt.text = "Loading...";
			SQLData.mainClip = MovieClip(this);
			
			LoginForm.visible=false;
			stop();
		}
		function initGame()
		{
			trace("initGame");
			GameMenu.MapSwapPanel.x = 130;
			GameMenu.PokedexPanel.x = 130;
			GameMenu.PokemonPanel.x = 130;
			GameMenu.PVPPanel.x = 130;
			//Trainer.addEventListener
			enableAllMenu();
			Trainer.gotoAndPlay("standing");
			flagCheck = setInterval(checkMultiTab,5000);//SQL

			mapSwap(curentMap,false,SQLData.miles,false);
			loadPokemonList();
			LoadItemList();
			setNewInforPanel();
		}
		function addPokemonAuto()
		{
			var randomPokemonNumber = Math.round(Math.random() * (totalPokemonInMap - 1));
			if (wildPokemonAppear(mapPokemon.pkml.child(randomPokemonNumber).wrate))
			{
				wildPokemonList = newRivalPokemonList(mapPokemon.pkml.child(randomPokemonNumber));
				BeforeBattle();
				BlankMC.addChild(new PokemonBattle(wildPokemonList,false,SQLData.miles));
			}
		}
		function addNewFloatText(str:String,xx:Number,yy:Number)
		{
			addChild(new FloatingText(str,xx,yy));
		}
		function setNewInforPanel()
		{
			//Trace("Set! "+SQLData.pmp);
			GameMenu.PMP.text = String(SQLData.pmp);
			GameMenu.Miles.text = String(SQLData.miles);
		}

		function removeEventWalking()
		{
			if (menuIsShowed)
			{
				showMenu();
			}
			//Trace("removeEventWalking");
			clearInterval(intervalId);
			Trainer.gotoAndPlay("standing");
			trainerIsStanding = true;
			stage.removeEventListener(KeyboardEvent.KEY_DOWN,getKeyDown);
			stage.removeEventListener(KeyboardEvent.KEY_UP,getKeyUpEvent);
			removeEventListener(Event.ENTER_FRAME,worldObjectMoving);
		}
		function addEventWalking()
		{
			//Trace("addEventWalking");
			stage.addEventListener(KeyboardEvent.KEY_DOWN,getKeyDown);
			stage.addEventListener(KeyboardEvent.KEY_UP,getKeyUpEvent);
		}
		function newRivalPokemonList(PokemonList:XMLList,isMany:Boolean=false):Array
		{
			var ArrayObject=new Array();
			if (isMany)
			{
				for (var i:int=0; i<PokemonList.pkm.length(); i++)
				{
					ArrayObject.push(SQLData.NewPokemonObjectFromXMLList(new XMLList(PokemonList.pkm[i])));
				}
			}
			else
			{
				ArrayObject.push(SQLData.NewPokemonObjectFromXMLList(PokemonList));
			}
			return ArrayObject;
		}
		function addEventObjectWithBattle(mile:Number,XMLData:XMLList,isBattle:Boolean=true,isMany:Boolean=false):Array
		{
			var ArrayObject=new Array();
			var counti:int = 0;
			var EventL:Number;
			var EventN:Number;
			for (var i:int=0; i<XMLData.children().length(); i++)
			{
				EventL = Number(XMLData.child(i).evl);
				EventN = Number(XMLData.child(i).evn);
				if (Number(XMLData.child(i).mile)==mile &&checkGameEvent(EventL,EventN))
				{
					if (isBattle)
					{
						if (isMany)
						{
							ArrayObject.push(new PokemonEventObject(newRivalPokemonList(new XMLList(XMLData.child(i)),true)));
						}
						else
						{
							ArrayObject.push(new PokemonEventObject(newRivalPokemonList(new XMLList(XMLData.child(i)))));
						}
					}
					else
					{
						//Trace("Here");
						ArrayObject.push(new NPCMessenger(new XMLList(XMLData.child(i))));
					}
					ArrayObject[counti].x = 500 + Number(XMLData.child(i).xpos);
					ArrayObject[counti].setEvent(EventL,EventN);
					RootWorldObject.NPCObject.addChild(ArrayObject[counti]);
					ArrayObject[counti].addMe();
					counti++;
				}
			}
			return ArrayObject;
		}
		function checkGameEvent(objectEventLocation:Number,objectEventNumber:Number):Boolean
		{
			if (objectEventNumber==0)
			{
				return true;
			}
			if (gameEventList[objectEventLocation] == objectEventNumber)
			{
				return true;
			}
			return false;
		}
		function addWorldEventObject(mile:Number)
		{
			while (RootWorldObject.NPCObject.numChildren > 0)
			{
				RootWorldObject.NPCObject.removeChildAt(0);
			}
			pokemonEventList = addEventObjectWithBattle(mile,mapPokemon.pkmevt);
			trainerEventList = addEventObjectWithBattle(mile,mapPokemon.trainer,true,true);
			npcEventList = addEventObjectWithBattle(mile,mapPokemon.npcchat,false);

			var npseller:NPCSell=new NPCSell();
			RootWorldObject.NPCObject.addChild(npseller);
			npseller.addMe();
		}
		function worldObjectMoving(e:Event)
		{
			if ( !(speed<0 && pixelCount<=6&& SQLData.miles<=1)  )
			{
				//if(SQLData.miles>0){
				for (var i:int =0; i < trainerEventList.length; i++)
				{
					if (Trainer.hitTestObject(trainerEventList[i]))
					{
						if (menuIsShowed)
						{
							showMenu();
						}
						loadBattle(trainerEventList[i].Pokemon,true);
						trainerEventList[i].hitMe();
						break;
					}
				}
				pixelCount +=  speed;
				RootGround.x -=  speed;
				RootWorldObject.x -=  speed;
				if ((pixelCount>3234 || pixelCount<0))
				{
					if (newMap!=curentMap)
					{
						mapSwap(curentMap,false,SQLData.miles);
					}
					else
					{
						if (pixelCount>3234)
						{
							pixelCount = 0;
							RootGround.x = 0;
							RootWorldObject.x = 0;
							SQLData.miles++;
						}
						else
						{
							pixelCount = 3234;
							RootGround.x = -3234;
							RootWorldObject.x = -3234;
							SQLData.miles--;
						}
						SQLData.ChangeMap(curentMap,SQLData.miles);
						setNewInforPanel();
						addWorldEventObject(SQLData.miles);
					}
				}
			}
		}
		function mapSwap(mapName:String, mapEvent:Boolean, sMiles:Number, reco:Boolean=true)
		{
			//Trace("mapName" +mapName)
			mapPokemon = SQLData.mapsXML[mapName];
			totalPokemonInMap = mapPokemon.pkml.pkm.length();
			newMap = mapName;
			addWorldEventObject(sMiles);
			if (! mapEvent)
			{
			//Trace("mapEvent" +mapName)
				//trace("recoveryAllPokemon()");
				SQLData.miles = sMiles;
				curentMap = mapName;
				if (reco)
				{
					RecoveryAllPokemon();
					//Trace("recoveryAllPokemon");
					SQLData.ChangeMap(curentMap,SQLData.miles);
				}
			}
			freeTween();
			tweenList.unshift(new Tween (RootWorldObject,"y",Strong.easeOut,RootWorldObject.y,RootWorldObject.y+300,0.5,true));
			tweenList.unshift(new Tween (RootWorldObject,"alpha",Strong.easeOut,1,0,1,true));
			tweenList[0].addEventListener(TweenEvent.MOTION_FINISH,gotoNewMap);
			function gotoNewMap(evt:Event)
			{
			//Trace("gotoNewMap" +mapName)
				pixelCount = 0;
				RootWorldObject.x = 0;
				RootWorldObject.gotoAndStop(mapName);
				tweenList.unshift(new Tween (RootWorldObject,"y",Strong.easeOut,RootWorldObject.y,179,0.5,true));
				tweenList.unshift(new Tween (RootGround,"x",Strong.easeOut,RootGround.x,0,0.5,true));
				tweenList.unshift(new Tween (RootWorldObject,"alpha",Strong.easeOut,0,1,1,true));
				tweenList[0].addEventListener(TweenEvent.MOTION_FINISH,enableMenuEvent);
				addEventWalking();
			}
		}
		function getKeyUpEvent(evt:KeyboardEvent)
		{
			getKeyUp();
		}
		function getKeyUp()
		{
			clearInterval(intervalId);
			Trainer.gotoAndPlay("standing");
			trainerIsStanding = true;
			removeEventListener(Event.ENTER_FRAME,worldObjectMoving);
		}
		function getKeyDown(evt:KeyboardEvent)
		{
			var j:int;
			if (trainerIsStanding && evt.keyCode!=Keyboard.UP)
			{
				addEventListener(Event.ENTER_FRAME,worldObjectMoving);
				intervalId = setInterval(addPokemonAuto,2000);
				Trainer.gotoAndPlay("walking");
				trainerIsStanding = false;
				if (Math.round(Math.random() * 10) == 1)
				{
					addPokemonAuto();
				}
				for (j =0; j < npcEventList.length; j++)
				{
					npcEventList[j].unHitMe();
				}
			}
			switch (evt.keyCode)
			{
				case Keyboard.LEFT :
					speed =  -  speedPow;
					trainerIsGoingLeft = true;
					trainerIsGoingRight = false;
					Trainer.scaleX = -1;
					break;
				case Keyboard.RIGHT :
					speed = speedPow;
					trainerIsGoingLeft = false;
					trainerIsGoingRight = true;
					Trainer.scaleX = 1;
					break;
				case Keyboard.UP :
					speed = 0;
					getKeyUp();
					for (var i:int =0; i < pokemonEventList.length; i++)
					{
						if (Trainer.hitTestObject(pokemonEventList[i]))
						{
							if (menuIsShowed)
							{
								showMenu();
							}
							BeforeBattle();
							var newPokemonBattele:PokemonBattle = new PokemonBattle(pokemonEventList[i].Pokemon,true);
							newPokemonBattele.SetEventBattle(pokemonEventList[i].eventLocation,pokemonEventList[i].eventNumber);
							BlankMC.addChild(newPokemonBattele);
							pokemonEventList[i].hitMe();
							break;
						}
					}


					for (j =0; j < npcEventList.length; j++)
					{
						if (Trainer.hitTestObject(npcEventList[j]))
						{
							if (menuIsShowed)
							{
								showMenu();
							}
							npcEventList[j].hitMe();
							//changeUserEvent(npcEventList[j].eventLocation,npcEventList[j].eventNumber);
							break;
						}
					}
					break;
			}
		}

		//////////////////////////////PRELOAD
		function preLoad(evt:Event)
		{
			var total:Number = loaderInfo.bytesTotal;
			var loaded:Number = loaderInfo.bytesLoaded;
			preLoadtxt.text=Math.round(loaded/total*100).toString()+"%";
			if (total==loaded)
			{
				removeEventListener(Event.ENTER_FRAME,preLoad);
				preLoadtxt.text = "Loading Moves...";
				//loadXML("twalk_xmldata.php?xdata=Moves",loadXMLMoves);
				loadXML("Moves.xml",loadXMLMoves);
			}
		}
		function loadXML(link:String, myfun:Function)
		{
			var xmlLoader:URLLoader = new URLLoader();
			xmlLoader.dataFormat = new XML  ;
			xmlLoader.load(new URLRequest(link));
			xmlLoader.addEventListener(Event.COMPLETE, myfun);
		}
		function loadXMLMoves(evt:Event)
		{
			SQLData.movesXML = new XML(evt.target.data);
			//trace(movesXML.NightShade.name());
			preLoadtxt.text = "Loading Pokemon...";
			//loadXML("twalk_xmldata.php?xdata=ItemList",loadXMLPokemons);
			loadXML("ItemList.xml?"+Math.round(Math.random()*1000),loadXMLItems);
		}
		function loadXMLItems(evt:Event)
		{
			SQLData.itemsXML = new XML(evt.target.data);
			preLoadtxt.text = "Loading Item...";
			//loadXML("twalk_xmldata.php?xdata=Pokemons",loadXMLPokemons);
			loadXML("Pokemons.xml",loadXMLPokemons);
		}
		function loadXMLPokemons(evt:Event)
		{
			SQLData.pokemonsXML = new XML(evt.target.data);
			preLoadtxt.text = "Loading Map...";
			//trace(pokemonsXML["Machamp"]);
			//trace(pokemonsXML.child(1));mappkm.php
			loadXML("twalk_xmldata.php?xdata=MapPokemons",loadXMLMaps);
			//loadXML("MapPokemons.xml",loadXMLMaps);
		}
		function loadXMLMaps(evt:Event)
		{
			SQLData.mapsXML = new XML(evt.target.data);
			mapEvent = SQLData.mapsXML.MapEvent;
			preLoadtxt.text = "";
			LoginForm.visible=true;
			LoginForm.LoginButton.addEventListener(MouseEvent.CLICK,DoLoginForm);
			function DoLoginForm(e:MouseEvent){
				SQLData.username=LoginForm.LoginUsername.text;
				SQLData.password=LoginForm.LoginPassword.text;
				preLoadtxt.text = "Loging......";
				SQLData.GetData("Login",dataLogin);				
			}
		}
		function Trace(st:String)
		{
			traceText.text = st + "\n" + traceText.text;
		}
		function ReloadPokemonList(evt:Event):void
		{
			var XMLData = new XML(evt.target.data);			
				SQLData.AddPokemonFromXML(XMLData.lpkm,reloadMode,leadpkm);
				SQLData.AddItemFromXML(XMLData.litem);
		}
		function dataLogin(evt:Event):void
		{
			var XMLData = new XML(evt.target.data);
			xmlDisplay.text = XMLData.toString();
			preLoadtxt.text = "DANG KY THANH CONG";
			username = XMLData.username;
			uid = XMLData.uid;
			pmp = XMLData.upmp;
			leadpkm = XMLData.leadpkm;
			SQLData.setID(uid,usecurity,pmp);
			uflag = XMLData.uflag;
			SQLData.miles = Number(XMLData.Mile);
			if (String(XMLData.Map)=="")
			{
				curentMap = "SheepRanch";
				newMap = "SheepRanch";
			}
			else
			{
				curentMap = String(XMLData.Map);
				newMap = String(XMLData.Map);//
			}
			//Trace("Flag "+uflag);
			if (uflag=="0")
			{
				preLoadtxt.text = "BAN CHUA DANG NHAP VAO DIEN DAN...";
			}
			if (uflag=="2")//Chưa đăng ký
			{
				Trace("Get Flag 2");
				preLoadtxt.text = "DANG KY THANH CONG";
				SQLData.GetData("Login",dataLogin);
			}
			if (uflag=="1")
			{
				SQLData.AddPokemonFromXML(XMLData.lpkm,reloadMode,leadpkm);
				SQLData.AddItemFromXML(XMLData.litem);
				addEventLocal(XMLData.eventlist);
				//pokemonList=SQLData.pokemonList;
				//pokemonAllList=SQLData.pokemonAllList;
				gotoAndStop("game");
			}
		}


		function addEventLocal(EventSQLList:XMLList)
		{//SQL
			var evtlist:Number = EventSQLList.evt.length();
			for (var i:int =1; i<evtlist; i++)
			{
				gameEventList[i] = Number(EventSQLList.evt[i]);
			}
		}



		function changeEvent(eventLocation:Number,eventNumber:Number)
		{
			gameEventList[eventLocation] = eventNumber;
		}

		/////////////////////////////////Function

		function freeTween()
		{
			tweenList = null;
			tweenList=new Array();
		}
		function checkMultiTab()
		{
			SQLData.SendQuery("","CheckSec");
			if (SQLData.uflag != "1")
			{
				gotoAndStop("flag");
				removeEventWalking();
				clearInterval(flagCheck);
			}
		}

		function wildPokemonAppear(wrate:String):Boolean
		{
			var aRate:Number;
			if (wrate=="")
			{
				aRate = 90;
			}
			else
			{
				aRate = Number(wrate);
			}
			if (notMiss(aRate))
			{
				return true;
			}
			return false;
		}

		function notMiss(Accuracy:Number):Boolean
		{
			if (Math.round(Math.random() * 100) > Accuracy)
			{
				return false;
			}
			else
			{
				return true;
			}
		}
		function BeforeBattle()
		{
			hideAllPanel();
			disableAllMenu();
			removeEventWalking();
		}
		function AfterBattle(_case:String)
		{
				//Trace("...EnableEventAfterBattle....");
			enableAllMenu();
			switch (_case){
				case "catched":
					addEventWalking();	
					Trainer.addChild(new FloatingText("BO CHAY...",0,0,2));
					reloadMode = "insert";
					SQLData.GetData("Login",ReloadPokemonList);		
					break;
				case "flee":
					addEventWalking();	
					Trainer.addChild(new FloatingText("BO CHAY...",0,0,2));
					break;
				case "win":
					addEventWalking();			
					Trainer.addChild(new FloatingText("CHIEN THANG!",0,0,2));	
					break;
				case "failed":
					Trainer.addChild(new FloatingText("BAN DA THUA!",0,0,2));
					mapSwap(curentMap,false,1);
					break;
			}
		}
		function loadBattle(rivalPokemonList:Array,isTrainer:Boolean=false)
		{
			BeforeBattle();
			//rivalPokemon=Pokemon;
			BlankMC.addChild(new PokemonBattle(rivalPokemonList,isTrainer));
		}
		function getPokemonIDFromName(pokemonName:String):Number
		{
			return Number(SQLData.pokemonsXML[pokemonName].No);
		}

		function enableAllMenu()
		{
			ShowAllMenu.addEventListener(MouseEvent.CLICK,showMenuEvent);
		}
		function disableAllMenu()
		{
			ShowAllMenu.removeEventListener(MouseEvent.CLICK,showMenuEvent);
		}
		function RecoveryAllPokemon()
		{
			for (var i:int=0; i<SQLData.pokemonList.length; i++)
			{
				SQLData.pokemonList[i].Recovery();
				SQLData.UpdatePokemonSQL(SQLData.pokemonList[i]);
			}
		}
		function removeAllEvent()
		{
			disableMenu();
		}

		function loadRank()
		{
			GameMenu.RankPanel.GridData.removeAllColumns();
			GameMenu.RankPanel.GridData.dataProvider.removeAll();
			GameMenu.RankPanel.GridData.addColumn("Trainer");
			GameMenu.RankPanel.GridData.addItem({Trainer:"DragonAce"});
		}
		function enableMenuEvent(e:Event)
		{
			trace("Swap Ok!");
			enableMenu();
		}
		function enableMenu()
		{
			GameMenu.ShowRank.addEventListener(MouseEvent.CLICK,showRankEvent);
			GameMenu.ShowPokedex.addEventListener(MouseEvent.CLICK,showPokedexEvent);
			GameMenu.ShowMapSwap.addEventListener(MouseEvent.CLICK,showMapSwapEvent);
			GameMenu.ShowPokemon.addEventListener(MouseEvent.CLICK,showPokemonEvent);
			GameMenu.ShowPVP.addEventListener(MouseEvent.CLICK,showPVPEvent);
			GameMenu.ShowItem.addEventListener(MouseEvent.CLICK,showItemEvent);

			GameMenu.MapSwapPanel.addEventListener(MouseEvent.CLICK,clickMapSwap);
		}
		function disableMenu()
		{
			GameMenu.ShowRank.removeEventListener(MouseEvent.CLICK,showRankEvent);
			GameMenu.ShowPokedex.removeEventListener(MouseEvent.CLICK,showPokedexEvent);
			GameMenu.ShowMapSwap.removeEventListener(MouseEvent.CLICK,showMapSwapEvent);
			GameMenu.ShowPokemon.removeEventListener(MouseEvent.CLICK,showPokemonEvent);
			GameMenu.ShowPVP.removeEventListener(MouseEvent.CLICK,showPVPEvent);
			GameMenu.ShowItem.removeEventListener(MouseEvent.CLICK,showItemEvent);

			GameMenu.MapSwapPanel.removeEventListener(MouseEvent.CLICK,clickMapSwap);
		}
		function gameMenu(evt:Event)
		{
			if (menuIsShowed)
			{
			}
			else
			{
			}
		}
		function hideAllPanel()
		{
			hidePVPPanel();
			hideItemPanel();
			hidePokemonPanel();
			hidePokedexPanel();
			hideRankPanel();
			hideMapSwap();
		}
		function clickMapSwap(evt:MouseEvent)
		{
			mapSwap(evt.target.name,false,1);
		}
		function showMenuEvent(evt:Event)
		{
			showMenuManual();
		}
		function showMenuManual()
		{
			showMenu();
		}
		function showMenu()
		{
			if (! menuIsShowed)
			{
				setNewInforPanel();
				enableMenu();
				ShowAllMenu.scaleX = -1;
				ShowAllMenu.x = 40;
				freeTween();
				tweenList.unshift(new Tween (GameMenu,"x",Strong.easeOut,-135,8,1,true));
				tweenList.unshift(new Tween (GameMenu,"alpha",Strong.easeOut,0,1,1,true));
				tweenList.unshift(new Tween (ShowAllMenu,"y",Strong.easeOut,4,182,1,true));
				menuIsShowed = true;
			}
			else
			{
				disableMenu();
				ShowAllMenu.scaleX = 1;
				ShowAllMenu.x = 4;
				tweenList.unshift(new Tween (GameMenu,"x",Strong.easeOut,8,-135,1,true));
				tweenList.unshift(new Tween (GameMenu,"alpha",Strong.easeOut,1,0, 1,true));
				tweenList.unshift(new Tween (ShowAllMenu,"y",Strong.easeOut,182,0, 1,true));
				hideAllPanel();
				menuIsShowed = false;
			}
		}
		function showMapSwap()
		{
			if (! mapswapIsShowed)
			{
				hideAllPanel();
				freeTween();
				tweenList.unshift(new Tween (GameMenu.MapSwapPanel,"y",Strong.easeOut,-272,-2,1,true));
				mapswapIsShowed = true;

			}
			else
			{
				hideMapSwap();
			}
		}
		function showMapSwapEvent(evt:Event)
		{
			showMapSwap();
		}
		function hideMapSwap()
		{
			if (mapswapIsShowed)
			{
				freeTween();
				tweenList.unshift(new Tween (GameMenu.MapSwapPanel,"y",Strong.easeOut,-2,-272,1,true));
				mapswapIsShowed = false;
			}
		}
		function showRank()
		{
			if (! rankIsShowed)
			{
				hideAllPanel();
				loadRank();
				freeTween();
				tweenList.unshift(new Tween (GameMenu.RankPanel,"y",Strong.easeOut,-272,-2,1,true));
				rankIsShowed = true;
			}
			else
			{
				hideRankPanel();
			}
		}
		function showRankEvent(evt:Event)
		{
			showRank();
		}
		function hideRankPanel()
		{
			if (rankIsShowed)
			{
				freeTween();
				tweenList.unshift(new Tween (GameMenu.RankPanel,"y",Strong.easeOut,-2,-272,1,true));
				rankIsShowed = false;
			}
		}
		function showPokedex()
		{
			if (! pokedexIsShowed)
			{
				hideAllPanel();
				freeTween();
				tweenList.unshift(new Tween (GameMenu.PokedexPanel,"y",Strong.easeOut,-272,-2,1,true));
				pokedexIsShowed = true;
			}
			else
			{
				hidePokedexPanel();
			}
		}
		function showPokedexEvent(evt:Event)
		{
			showPokedex();
		}
		function hidePokedexPanel()
		{
			if (pokedexIsShowed)
			{
				freeTween();
				tweenList.unshift( new Tween (GameMenu.PokedexPanel,"y",Strong.easeOut,-2,-272,1,true));
				pokedexIsShowed = false;
			}
		}
		function showPokemon()
		{
			if (! pokemonIsShowed)
			{
				loadPokemonList();
				hideAllPanel();
				freeTween();
				tweenList.unshift(new Tween (GameMenu.PokemonPanel,"y",Strong.easeOut,-272,-2,1,true));
				pokemonIsShowed = true;
			}
			else
			{
				hidePokemonPanel();
			}
		}
		function showPokemonEvent(evt:Event)
		{
			showPokemon();
		}
		function hidePokemonPanel()
		{
			if (pokemonIsShowed)
			{
				freeTween();
				tweenList.unshift( new Tween (GameMenu.PokemonPanel,"y",Strong.easeOut,-2,-272,1,true));
				pokemonIsShowed = false;
			}
		}

		function showPVP()
		{
			if (! PVPIsShowed)
			{
				hideAllPanel();
				freeTween();
				tweenList.unshift(new Tween (GameMenu.PVPPanel,"y",Strong.easeOut,-300,-2,1,true));
				PVPIsShowed = true;
				GameMenu.PVPPanel.PVPAI.addEventListener(MouseEvent.CLICK,DoPVPAIEvent);
				GameMenu.PVPPanel.PVPPVP.addEventListener(MouseEvent.CLICK,DoPVPPVPEvent);
			}
			else
			{
				hidePVPPanel();
			}
		}
		function showPVPEvent(evt:Event)
		{
			showPVP();
		}
		function hidePVPPanel()
		{
			if (PVPIsShowed)
			{
				freeTween();
				tweenList.unshift( new Tween (GameMenu.PVPPanel,"y",Strong.easeOut,-2,-300,1,true));
				PVPIsShowed = false;
				GameMenu.PVPPanel.PVPAI.removeEventListener(MouseEvent.CLICK,DoPVPAIEvent);
				GameMenu.PVPPanel.PVPPVP.removeEventListener(MouseEvent.CLICK,DoPVPPVPEvent);
			}
		}
		function DoPVPPVPEvent(e:MouseEvent){
			GameMenu.PVPPanel.PVPLog.text="LOADING";
			SQLData.GetData("GetPVPPokemonList",DoPVPPVP,GameMenu.PVPPanel.PVPID.text);
		}
		function DoPVPPVP(e:Event){
			var AIPokemonListXML:XML = new XML(e.target.data);		
			xmlDisplay.text=AIPokemonListXML.toString();
			var AIPokemonList:Array = SQLData.PVPAddPokemonFromXML(AIPokemonListXML.lpkm,AIPokemonListXML.leadpkm);
			if(AIPokemonList.length>0){				
				mapSwap(curentMap,false,1);
				BeforeBattle();
				BlankMC.addChild(new PVPBattle(AIPokemonList,GameMenu.PVPPanel.PVPID.text));	
			}else{
				GameMenu.PVPPanel.PVPLog.text="ID KHONG TON TAI";
			}
		}		
		function DoPVPAskEvent(e:MouseEvent){
				atutoPvpRespond = setInterval(checkMultiTab,200);//SQL
				GameMenu.PVPPanel.PVPLog.text="DANG CHO PHAN HOI";			
		}
/*		function checkAutoPVPRespond(){
			SQLData.GetData("AskPVP",AskPVPRespond,GameMenu.PVPPanel.PVPID.text);				
		}
		function checkAutoPVPAsk(){
			SQLData.GetData("AskPVP",AskPVP);			
		}
		function AskPVP(e:Event){
			var XMLData:XML = new XML(e.target.data);	
			if(XMLData.PVPAsk!="0"){		
				PVPAskButton.visible=true;
				PVPAskButton.addEventListener(MouseEvent.CLICK,okPVP);
				function okPVP(e:MouseEvent){
					PVPAskButton.visible=false;
					
				}
			}			
		}
		function AskPVPRespond(e:Event){
			var XMLData:XML = new XML(e.target.data);	
			if(XMLData.PVPID==SQLData.uid){			
				mapSwap(curentMap,false,1);
				BeforeBattle();
				BlankMC.addChild(new PVPBattle(AIPokemonList,GameMenu.PVPPanel.PVPID.text));		
			}			
		}*/
		/*
				function DoPVPAskEvent(e:MouseEvent){
			atutoPvpRespond = setInterval(checkAutoPVPRespond,200);//SQL
			GameMenu.PVPPanel.PVPLog.text="DANG CHO PHAN HOI";			
		}
		function checkAutoPVPRespond(){
			SQLData.GetData("AskPVP",AskPVPRespond,GameMenu.PVPPanel.PVPID.text);				
		}
		function AskPVPRespond(e:Event){
			var XMLData:XML = new XML(e.target.data);	
			if(XMLData.PVPID==SQLData.uid){			
				PVPGetRivalData()
			}			
		}
		function PVPGetRivalData(){
			SQLData.GetData("GetPVPPokemonList",StartPVPBattle,GameMenu.PVPPanel.PVPID.text);
		}
		function StartPVPBattle(e:Event){
			var AIPokemonListXML:XML = new XML(e.target.data);		
			xmlDisplay.text=AIPokemonListXML.toString();
			var AIPokemonList:Array = SQLData.PVPAddPokemonFromXML(AIPokemonListXML.lpkm,AIPokemonListXML.leadpkm);
			if(AIPokemonList.length>0){				
				mapSwap(curentMap,false,1);
				BeforeBattle();
				BlankMC.addChild(new PVPBattle(AIPokemonList,GameMenu.PVPPanel.PVPID.text));		
			}else{
				GameMenu.PVPPanel.PVPLog.text="ID KHONG TON TAI";
			}
		}		
		
		function checkAutoPVPAsk(){
			SQLData.GetData("AskPVP",CheckAskPVP);			
		}
		function CheckAskPVP(e:Event){
			var XMLData:XML = new XML(e.target.data);	SQLData.mainClip.Trace("XMLData.PVPAsk "+XMLData.PVPAsk);
			if(XMLData.PVPAsk!="0" && XMLData.PVPAsk!="" && XMLData.PVPAsk!=0){		
				PVPAskButton.visible=true;
				PVPAskButton.addEventListener(MouseEvent.CLICK,okPVP);
				function okPVP(e:MouseEvent){
					PVPAskButton.visible=false;
					SQLData.SendQuery("UPDATE twalk_user SET PVPID='"+XMLData.PVPAsk+"' WHERE uid=='"+SQLData.uid+"'");
					SQLData.GetData("GetPVPPokemonList",StartPVPBattle,XMLData.PVPAsk);  
				}
			}else{
				PVPAskButton.visible=false;				
			}
		}
		function DoPVPAIEvent(e:MouseEvent){
			GameMenu.PVPPanel.PVPLog.text="LOADING";
			SQLData.GetData("GetPVPPokemonList",DoPVPAI,GameMenu.PVPPanel.PVPID.text);
		}
		function DoPVPAI(e:Event){
			var AIPokemonListXML:XML = new XML(e.target.data);		
			xmlDisplay.text=AIPokemonListXML.toString();
			//var AIPokemonList:Array = newRivalPokemonList(AIPokemonListXML.lpkm,true);
			var AIPokemonList:Array = SQLData.PVPAddPokemonFromXML(AIPokemonListXML.lpkm,AIPokemonListXML.leadpkm);
			SQLData.mainClip.Trace("AIPokemonList "+AIPokemonList.length);
			if(AIPokemonList.length>0){
				mapSwap(curentMap,false,1);
				BeforeBattle();
				BlankMC.addChild(new PokemonBattle(AIPokemonList,true));				
			}else{
				GameMenu.PVPPanel.PVPLog.text="ID KHONG TON TAI";
			}
		}
		*/
		function DoPVPAIEvent(e:MouseEvent){
			GameMenu.PVPPanel.PVPLog.text="LOADING";
			SQLData.GetData("GetPVPPokemonList",DoPVPAI,GameMenu.PVPPanel.PVPID.text);
		}
		function DoPVPAI(e:Event){
			var AIPokemonListXML:XML = new XML(e.target.data);		
			xmlDisplay.text=AIPokemonListXML.toString();
			//var AIPokemonList:Array = newRivalPokemonList(AIPokemonListXML.lpkm,true);
			var AIPokemonList:Array = SQLData.PVPAddPokemonFromXML(AIPokemonListXML.lpkm,AIPokemonListXML.leadpkm);
			SQLData.mainClip.Trace("AIPokemonList "+AIPokemonList.length);
			if(AIPokemonList.length>0){
				mapSwap(curentMap,false,1);
				BeforeBattle();
				BlankMC.addChild(new PokemonBattle(AIPokemonList,true));				
			}else{
				GameMenu.PVPPanel.PVPLog.text="ID KHONG TON TAI";
			}
		}
		function showItem()
		{
			if (! itemIsShowed)
			{
				LoadItemList();
				hideAllPanel();
				freeTween();
				tweenList.unshift(new Tween (GameMenu.ItemPanel,"y",Strong.easeOut,-272,-2,1,true));
				itemIsShowed = true;
			}
			else
			{
				hideItemPanel();
			}
		}
		function showItemEvent(evt:Event)
		{
			showItem();
		}
		function hideItemPanel()
		{
			if (itemIsShowed)
			{
				freeTween();
				tweenList.unshift( new Tween (GameMenu.ItemPanel,"y",Strong.easeOut,-2,-272,1,true));
				itemIsShowed = false;
			}
		}

		function loadPokemonList()
		{
			while (GameMenu.PokemonPanel.numChildren > 0)
			{
				GameMenu.PokemonPanel.removeChildAt(0);
			}
			for (var i:int =0; i<SQLData.pokemonList.length; i++)
			{
				var pokemon:PokemonInfor = new PokemonInfor(SQLData.pokemonList[i]);
				pokemon.y = 24 * i;
				pokemon.AddEventSwitchPokemon();
				GameMenu.PokemonPanel.addChild(pokemon);
			}
		}
		function LoadItemList()
		{			
			SQLData.LoadItemPanel(GameMenu.ItemPanel);
		}		
	}
}